﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace gptDataType.Helper
{
    public class CheckCVariableName
    {
        // C语言关键字列表
        private static readonly List<string> cKeywords = new List<string>
    {
        "auto", "break", "case", "char", "const", "continue", "default",
        "do", "double", "else", "enum", "extern", "float", "for",
        "goto", "if", "int", "long", "register", "return", "short",
        "signed", "sizeof", "static", "struct", "switch", "typedef",
        "union", "unsigned", "void", "volatile", "while"
    };

        static bool IsValidCVariableName(string name)
        {
            // 检查是否为空
            if (string.IsNullOrEmpty(name))
            {
                return false;
            }

            // 检查是否以数字开头
            if (char.IsDigit(name[0]))
            {
                return false;
            }

            // 检查是否包含非法字符
            if (!Regex.IsMatch(name, @"^[a-zA-Z_][a-zA-Z0-9_]*$"))
            {
                return false;
            }

            // 检查是否为C语言关键字
            if (cKeywords.Contains(name.ToLower()))
            {
                return false;
            }

            return true;
        }

        //static void Main()
        //{
        //    string[] testNames = { "validName", "1invalid", "if", "valid_Name_1" };
        //    foreach (string name in testNames)
        //    {
        //        Console.WriteLine($"{name}: {IsValidCVariableName(name)}");
        //    }
        //}
    }
}
